import query from '@dfeidao/atom-nodejs/db/query';

import operate from '@dfeidao/atom-nodejs/db/operate';
// import { systemid } from './config';
interface Message {
	_id: string;
	role_no: string;
	pk_val: string;
	productid: string;
}
interface Data {
	group_id: string;
}
export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const systemid = '12';

	const { prepare: p, exec: e } = query(action_id, session_id, systemid);
	const { del, exec } = operate(action_id, session_id, systemid);

	p('allot_conditions_group', ['group_id'], { productid: msg.productid, role_allot_id: msg._id }, 199, 1, [], []);

	const [data] = await e<Data>();
	const arrr = data.map((v) => {
		return {
			productid: msg.productid, group_id: v.group_id
		};
	});
	arrr.forEach((i) => {
		del('allot_conditions', i);

	});

	// const ret1 = await exec()
	del('allot_conditions_group', { productid: msg.productid, role_allot_id: msg._id });
	// const ret = await exec()

	del('pub_role_allot', { role_no: msg.role_no, productid: msg.productid, pk_val: msg.pk_val });
	const ret2 = await exec();
	if (ret2.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
